package cn.jbolt.admin.qiniu;

import cn.jbolt.core.api.JBoltApiBaseController;
import cn.jbolt.core.api.httpmethod.JBoltHttpGet;
import cn.jbolt.core.api.httpmethod.JBoltHttpPost;
import cn.jbolt.core.model.Qiniu;
import cn.jbolt.core.model.base.JBoltBaseModel;
import cn.jbolt.core.para.JBoltNoUrlPara;
import cn.jbolt.core.permission.CheckPermission;
import cn.jbolt.core.permission.OnlySaasPlatform;
import cn.jbolt.core.permission.UnCheck;
import cn.jbolt.core.permission.UnCheckIfSystemAdmin;
import cn.jbolt.core.service.JBoltQiniuService;
import cn.jbolt.core.service.base.JBoltBaseService;
import cn.jbolt.extend.permission.PermissionKey;
import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.Path;
import com.jfinal.core.paragetter.Para;
import com.jfinal.plugin.activerecord.tx.Tx;

@UnCheckIfSystemAdmin
@OnlySaasPlatform
@CheckPermission(PermissionKey.QINIU)
@Path("/api/admin/qiniu")
public class QiniuAdminController extends JBoltApiBaseController {

    @Inject
    private JBoltQiniuService service;

    @Override
    protected JBoltBaseService<? extends JBoltBaseModel<?>> getModelService() {
        return service;
    }

    /**
     * 单个详情
     */
    @JBoltHttpGet
    public void index() {
        renderJBoltApiRet(findModelById());
    }

    /**
     * 分页条件查询数据源
     * @param page
     * @param pageSize
     * @param keywords
     * @param type
     * @param isDefault
     * @param enable
     */
    @Before(JBoltNoUrlPara.class)
    @JBoltHttpGet
    public void datas(@Para(value = "page",defaultValue = "1")Integer page,
                      @Para(value = "pageSize",defaultValue = "15")Integer pageSize,
                      @Para("keywords")String keywords,
                      @Para("type")Integer type,
                      @Para("isDefault")Boolean isDefault,
                      @Para("enable")Boolean enable) {
        renderJBoltApiSuccessWithData(service.paginateAdminDatas(page,pageSize,keywords,type,isDefault,enable));
    }

    /**
     * 获得指定SN的七牛空间的上传参数
     * @param bucketSn 不传就使用默认
     */
    @UnCheck
    @Before(JBoltNoUrlPara.class)
    @JBoltHttpGet
    public void getUploadParas(@Para("sn")String bucketSn) {
        renderJBoltApiRet(service.genUploadParas(bucketSn));
    }

    /**
     * select数据源options
     */
    @UnCheck
    @JBoltHttpGet
    public void options() {
        renderJBoltApiSuccessWithData(service.getOptionListEnable());
    }



    /**
     * 保存
     */
    @Before(Tx.class)
    @JBoltHttpPost
    public void save(@Para("") Qiniu qiniu) {
        renderJBoltApiRet(service.save(qiniu));
    }

    /**
     * 更新
     */
    @Before(Tx.class)
    @JBoltHttpPost
    public void update(@Para("")Qiniu qiniu) {
        renderJBoltApiRet(service.update(qiniu));
    }

    /**
     * 删除
     */
    @Before(Tx.class)
    @JBoltHttpGet
    public void delete() {
        renderJBoltApiRet(service.delete(getId()));
    }

    /**
     * 切换启用状态
     */
    @Before(Tx.class)
    @JBoltHttpGet
    public void toggleEnable() {
        renderJBoltApiRet(service.toggleEnable(getId()));
    }

    /**
     * 切换是否默认
     */
    @Before(Tx.class)
    @JBoltHttpGet
    public void toggleIsDefault() {
        renderJBoltApiRet(service.toggleIsDefault(getId()));
    }

}
